import "./index.css"
const $loading = {};
$loading.stopBodyScroll = function(isFixed, that) {
	var bodyEl = document.body;
	if (isFixed) { //开启
		that._top = window.scrollY;
		bodyEl.style.position = 'fixed';
		bodyEl.style.top = -that._top + 'px';
	} else { //关闭
		bodyEl.style.position = 'relative';
		bodyEl.style.top = '';
		window.scrollTo(0, that._top); // 回到原先的top
	}
};
$loading.show = function(text) {
  text === undefined && (text = "正在加载")
	this.loadingMask = document.getElementById("loading-mask");
	this.stopBodyScroll(true, this);
	if (this.loadingMask) {
		this.loading.style.display = "block";
	} else {
		this.loading = document.createElement("div");
		this.str = "";
		for (var i = 0; i < 12; i++) {
			this.str += '<div class="loading_leaf loading_leaf_' + i + '"></div>'
		};
		this.html =
			'<div class="loading-mask" id="loading-mask"><div class="loading-wrapper"><div class="loading__loading-wrapper">' +
			this.str + '</div><div class="loading__loading-wrapper-text">'+ text +'</div></div></div>'
		this.loading.innerHTML = this.html;
		document.body.appendChild(this.loading);
	};
};
$loading.hide = function() {
	this.stopBodyScroll(false, this);
	try {
		this.loading.style.display = "none";
	} catch (e) {};
};

export default $loading